using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;

/// <summary>
/// The EmployeeContactTableDB class is responsible for interacting with the database to retrieve and store information
/// about EmployeeContactTable objects.
/// </summary>
public class EmployeeContactTableDB

{
#region "Public Methods"

/// <summary>
/// Gets an instance of EmployeeContactTable from the underlying datasource.
/// </summary>
/// <param name="employeeContactKey">The unique EmployeeContactKey of the EmployeeContactTable in the database.</param>
/// <returns>An EmployeeContactTable when the EmployeeContactKey was found in the database, or null otherwise.</returns>
public static EmployeeContactTable GetItem(int employeeContactKey)
{
EmployeeContactTable myEmployeeContactTable = null;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocEmployeeContactTableSelectSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@employeeContactKey", employeeContactKey);

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.Read()) {
myEmployeeContactTable = FillDataRecord(myReader);
}
myReader.Close();
}
myConnection.Close();
}
return myEmployeeContactTable;
}
}

/// <summary>
/// Returns a list with EmployeeContactTable objects.
/// </summary>
/// <returns>A generics List with the EmployeeContactTable objects.</returns>
public static EmployeeContactTableList GetList()
{
EmployeeContactTableList tempList = new EmployeeContactTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocEmployeeContactTableSelectList", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
}
return tempList;
}

/// <summary>
/// Returns a list with EmployeeContactTable objects.
/// </summary>
/// <returns>A generics List with the EmployeeContactTable objects.</returns>
public static EmployeeContactTableList GetListByUserAccountKey(int UserAccountKey)
{
EmployeeContactTableList tempList = new EmployeeContactTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocEmployeeContactTableSelectListByUserAccountKey", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@userAccountKey", UserAccountKey);
myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
return tempList;
}
}

public static EmployeeContactTable GetItemByUserAccountKey(int UserAccountKey)
{
    EmployeeContactTable myEmployeeContactTable = null;
    using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
    {
        using (SqlCommand myCommand = new SqlCommand("sprocEmployeeContactTableSelectListByUserAccountKey", myConnection))
        {
            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.Parameters.AddWithValue("@UserAccountKey", UserAccountKey);

            myConnection.Open();
            using (SqlDataReader myReader = myCommand.ExecuteReader())
            {
                if (myReader.Read())
                {
                    myEmployeeContactTable = FillDataRecord(myReader);
                }
                myReader.Close();
            }
            myConnection.Close();
        }
        return myEmployeeContactTable;
    }
}
/// <summary>
/// Saves a EmployeeContactTable in the database.
/// </summary>
/// <param name="myEmployeeContactTable">The EmployeeContactTable instance to save.</param>
/// <returns>The new EmployeeContactKey if the EmployeeContactTable is new in the database or the existing EmployeeContactKey when an item was updated.</returns>
public static int Save(EmployeeContactTable myEmployeeContactTable)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocEmployeeContactTableInsertUpdateSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

if (myEmployeeContactTable.EmployeeContactKey == -1)
{myCommand.Parameters.AddWithValue("@employeeContactKey", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@employeeContactKey", myEmployeeContactTable.EmployeeContactKey);
}
if (myEmployeeContactTable.UserAccountKey == null){
myCommand.Parameters.AddWithValue("@userAccountKey", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@userAccountKey", myEmployeeContactTable.UserAccountKey);
}
if (myEmployeeContactTable.MobileNumber == null){
myCommand.Parameters.AddWithValue("@mobileNumber", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@mobileNumber", myEmployeeContactTable.MobileNumber);
}
if (myEmployeeContactTable.HomePhoneNumber == null){
myCommand.Parameters.AddWithValue("@homePhoneNumber", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@homePhoneNumber", myEmployeeContactTable.HomePhoneNumber);
}
if (myEmployeeContactTable.OfficePhoneNumber == null){
myCommand.Parameters.AddWithValue("@officePhoneNumber", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@officePhoneNumber", myEmployeeContactTable.OfficePhoneNumber);
}
if (myEmployeeContactTable.FaxNumber == null){
myCommand.Parameters.AddWithValue("@faxNumber", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@faxNumber", myEmployeeContactTable.FaxNumber);
}
if (myEmployeeContactTable.OtherNumber == null){
myCommand.Parameters.AddWithValue("@otherNumber", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@otherNumber", myEmployeeContactTable.OtherNumber);
}
if (myEmployeeContactTable.EmailAddress == null){
myCommand.Parameters.AddWithValue("@emailAddress", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@emailAddress", myEmployeeContactTable.EmailAddress);
}
if (myEmployeeContactTable.Status == null){
myCommand.Parameters.AddWithValue("@status", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@status", myEmployeeContactTable.Status);
}
if (myEmployeeContactTable.CreatedDateTime == null){
myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@createdDateTime", myEmployeeContactTable.CreatedDateTime);
}
if (myEmployeeContactTable.ModifiedDateTime == null){
myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@modifiedDateTime", myEmployeeContactTable.ModifiedDateTime);
}

DbParameter returnValue;
returnValue = myCommand.CreateParameter();
returnValue.Direction = ParameterDirection.ReturnValue;
myCommand.Parameters.Add(returnValue);

myConnection.Open();
myCommand.ExecuteNonQuery();
result = Convert.ToInt32(returnValue.Value);
myConnection.Close();
}
}
return result;
}

/// <summary>
/// Deletes a EmployeeContactTable from the database.
/// </summary>
/// <param name="employeeContactKey">The EmployeeContactKey of the EmployeeContactTable to delete.</param>
/// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
public static bool Delete(int employeeContactKey)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocEmployeeContactTableDeleteSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myCommand.Parameters.AddWithValue("@employeeContactKey", employeeContactKey);
myConnection.Open();
result = myCommand.ExecuteNonQuery();
myConnection.Close();
}
}
return result > 0;
}

#endregion

/// <summary>
/// Initializes a new instance of the EmployeeContactTable class and fills it with the data fom the IDataRecord.
/// </summary>
private static EmployeeContactTable FillDataRecord(IDataRecord myDataRecord )
{
EmployeeContactTable myEmployeeContactTable = new EmployeeContactTable();
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("EmployeeContactKey")))
{
myEmployeeContactTable.EmployeeContactKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("EmployeeContactKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("UserAccountKey")))
{
myEmployeeContactTable.UserAccountKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("UserAccountKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("MobileNumber")))
{
myEmployeeContactTable.MobileNumber = myDataRecord.GetString(myDataRecord.GetOrdinal("MobileNumber"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("HomePhoneNumber")))
{
myEmployeeContactTable.HomePhoneNumber = myDataRecord.GetString(myDataRecord.GetOrdinal("HomePhoneNumber"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("OfficePhoneNumber")))
{
myEmployeeContactTable.OfficePhoneNumber = myDataRecord.GetString(myDataRecord.GetOrdinal("OfficePhoneNumber"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("FaxNumber")))
{
myEmployeeContactTable.FaxNumber = myDataRecord.GetString(myDataRecord.GetOrdinal("FaxNumber"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("OtherNumber")))
{
myEmployeeContactTable.OtherNumber = myDataRecord.GetString(myDataRecord.GetOrdinal("OtherNumber"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("EmailAddress")))
{
myEmployeeContactTable.EmailAddress = myDataRecord.GetString(myDataRecord.GetOrdinal("EmailAddress"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
{
myEmployeeContactTable.Status = myDataRecord.GetInt16(myDataRecord.GetOrdinal("Status"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
{
    myEmployeeContactTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
{
myEmployeeContactTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
}
return myEmployeeContactTable;
}


}
